<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
	 <meta charset="utf-8">
	 <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	 <title th:replace="common/base::pagetitle"></title>
	 <link th:replace="common/base::static"/>
</head>
<body style="background-color: #f2f2f2;">

<div id="kPage" class="layui-fluid" style="padding: 0">
	 <div class="layui-row">
		  <!-- 主要内容块 -->
		  <div class="layui-col-md12" id="kMainContentCol">
				<div class="layui-fluid wlb-fluid-padding15">
					 <div class="layui-card">
<!--						  <form>-->
<!--								<div class="layui-form layui-card-header wlb-card-header-auto">-->
<!--									 <div class="layui-form-item">-->
<!--										  <div class="layui-inline">-->
<!--												<label class="layui-form-label">签章流程id</label>-->
<!--												<div class="layui-input-inline">-->
<!--													 <input type="text" name="flowId" placeholder="请输入" autocomplete="off" class="layui-input">-->
<!--												</div>-->
<!--										  </div>-->
<!--										  <div class="layui-inline">-->
<!--												<button class="layui-btn " lay-submit="" lay-filter="kTableSearchFilter">-->
<!--													 <i class="layui-icon layui-icon-search"></i>-->
<!--												</button>-->
<!--												<button class="layui-btn " type="reset">-->
<!--													 <i class="layui-icon layui-icon-refresh-1"></i>-->
<!--												</button>-->
<!--										  </div>-->
<!--									 </div>-->
<!--								</div>-->
<!--						  </form>-->

						  <div class="layui-card-body">
								<table id="kContentTable" lay-filter="kContentTable"></table>
						  </div>
					 </div>

				</div>
		  </div>
	 </div>
</div>

<script th:replace="common/base::context"></script>
<script th:replace="common/base::mainjs"></script>

<script type="text/html" id="sealTypeTpl">
	 {{#  if(d.sealType == 1){ }}
	 <span class="layui-badge layui-bg-blue">机构模板章</span>
	 {{#  } else if(d.sealType == 2) { }}
	 <span class="layui-badge">个人模板章</span>
	 {{#  } else if(d.sealType == 3) { }}
	 <span class="layui-badge layui-bg-green">自定义印章</span>
	 {{#  } else { }}
	 <span class="layui-badge layui-bg-gray">未知</span>
	 {{# } }}
</script>

<!--头部工具栏模板-->
<script type="text/html" id="headToolbarTpl">
	 <button class="layui-btn layui-btn-sm" lay-event="add">上传</button>
</script>

<!--表格行工具栏模板-->
<script type="text/html" id="rowToolbarTpl">
	 {{#  if(!d.defaultFlag){ }}
	 <a class="layui-btn layui-btn-xs" lay-event="defs"><i class="layui-icon layui-icon-release"></i>设为默认</a>
	 {{# } }}
	 <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
	 
</script>

<script type="text/javascript" th:inline="javascript">
    layui.use(['element', 'form', 'layer', 'table'], function () {
        var elemet = layui.element, form = layui.form, layer = layui.layer, table = layui.table;
        var tableId = 'kContentTable';

        // 实例化table
        var tableIns = table.render($.extend(config.layui.table, {
            elem: '#' + tableId
            , url: BASE_WEB + 'seals/person/list/data' //数据接口
            , toolbar: '#headToolbarTpl'
            , cols: [[ //表头
                {type: 'checkbox', fixed: 'left'}
                , {field: 'accountId', title: '签章账号', width: 150}
                , {field: 'sealId', title: '印章ID', width: 150}
                , {field: 'sealType', title: '印章类型', width: 120, templet: '#sealTypeTpl'}
                , {field: 'height', title: '印章高度',  width: 100}
                , {field: 'width', title: '印章宽度',  width: 100}
                , {field: 'url', title: '印章下载', width: 120, templet: function (d){
                        return '<a class="layui-btn layui-btn-normal layui-btn-xs" onclick="downFile(\''+ d.url +'\')"><i class="layui-icon layui-icon-find-fill"></i>下载</a>'
						  }}
                , {field: 'defaultFlag', title: '是否默认印章', width: 120, templet: function (d){
                    return d.defaultFlag ? '是' : '否'
						  }}
                , {field: 'createDate', title: '创建时间', sort: true, width: 150, templet: function (d){
                        return layui.util.toDateString(new Date(d.createDate).getTime());
						  }}
                , {field: '', title: '操作', width: 325, fixed: 'right', templet: '#rowToolbarTpl'}
            ]]
        }));

        var area = ['490px', '540px'];

		// 监听表格头部事件
        table.on('toolbar(' + tableId + ')', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            var checkData = checkStatus.data; // 得到选中的数据
            switch (obj.event) {
                case 'add':
                    layer.open({
                        type: 2
                        , title: '上传印章'
                        , content: BASE_WEB + 'seals/person/edit'
                        , maxmin: true
                        , area: area   // 宽高
                        , btn: ['确定', '取消']
                        , yes: function (index, layero) {
                            //点击确认触发 iframe 内容中的按钮提交
                            var submit = layero.find('iframe').contents().find("#kFormSubmit");
                            submit.click();
                        }
                    });
                    break;
            }
        });
        // 表格行按钮 监听事件
        table.on('tool(' + tableId + ')', function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent === 'del') {
                layer.confirm('确定删除吗？', function (index) {
                    var param = {accountId: data.accountId, sealId: data.sealId};
                    //执行 Ajax 后重载
                    $.ajax({
                        type: req.type.post,
                        url: BASE_WEB + 'seals/person/delete',
                        data: param,
                        async: false,
                        success: function (r) {
                            if (r.code === req.status.ok) {
                                table.reload(tableId);
                                wlbKit.sucessMsg('已删除');
                            } else {
                                wlbKit.errorMsg(r.msg);
                            }
                        }
                    });
                });
            }
            if (layEvent === 'defs') {
                layer.confirm('确定设置为默认吗？', function (index) {
                    var param = {accountId: data.accountId, sealId: data.sealId};
                    //执行 Ajax 后重载
                    $.ajax({
                        type: req.type.post,
                        url: BASE_WEB + 'seals/person/default',
                        data: param,
                        async: false,
                        success: function (r) {
                            if (r.code === req.status.ok) {
                                table.reload(tableId);
                                wlbKit.sucessMsg('已设置为默认印章');
                            } else {
                                wlbKit.errorMsg(r.msg);
                            }
                        }
                    });
                });
            }
        });

        // 监听查询
        form.on('submit(kTableSearchFilter)', function (obj) {
            var params = obj.field;
            log('seach=', params);//
            tableIns.reload({
                where: params,
                page: {curr: 1}
            });
            return false;
        });

        // 绑定监听回车事件
        wlbKit.bindEnterEventForLayuiForm();

    });
    function downFile(url){
        const link = document.createElement('a');
        link.setAttribute('href', url); //设置下载文件的url地址
        //link.setAttribute('download', 'download'); //用于设置下载文件的文件名
        link.click();
	 }
</script>

</body>
</html>
